package sfeir.gwt.ergosum.client.profile;

import java.util.ArrayList;
import java.util.List;

import sfeir.gwt.ergosum.client.images.ErgoImages;
import sfeir.gwt.ergosum.client.images.NetworksImage;
import sfeir.gwt.ergosum.client.model.NetworkItem;
import sfeir.gwt.ergosum.client.model.Person;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FlexNetworkField extends Composite {

	private final ErgoImages ergoImages = GWT.create(ErgoImages.class);

    public static final String WEBSITE = "website";
    public static final String COMPANY = "company";
    public static final String BLOG = "blog";
    public static final String OTHER = "other";
    
	private VerticalPanel flexyWebsites;
	private VerticalPanel flexyNetworks;
	private Grid grid;
	private Image add = ergoImages.add().createImage();
	private ArrayList<NetworkItem> networks;
	private Person person;
	private VerticalPanel root;
	String[] stringsnetworks = new String[] { "linkedin", "twitter",
			"facebook", "viadeo", "flickr", "vimeo", "dopplr", "lastfm",
			"readernaut", "friendfeed" };
	private PushButton pb;
	private Image img = null;

	public FlexNetworkField() {

		Image icon = null;
		HTML title = null;

		icon = ergoImages.world().createImage();
		title = new HTML("URLs :");
		networks = new ArrayList<NetworkItem>();

		root = new VerticalPanel();

		grid = new Grid(1, 4);
		grid.setWidget(0, 0, icon);
		grid.setWidget(0, 1, title);

		flexyNetworks = new VerticalPanel();
		flexyWebsites = new VerticalPanel();
		addRow();

		grid.setWidget(0, 2, flexyWebsites);

		PushButton more = new PushButton(add);
		more.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				addRow();
			}
		});
		grid.setWidget(0, 3, more);

		/*
		 * Let's add some style now
		 */
		grid.getCellFormatter().setVerticalAlignment(0, 0,
				HasAlignment.ALIGN_TOP);
		grid.getCellFormatter().setVerticalAlignment(0, 1,
				HasAlignment.ALIGN_TOP);
		grid.getCellFormatter().setWidth(0, 1, "70px");
		grid.getCellFormatter().setHorizontalAlignment(0, 2,
				HasAlignment.ALIGN_LEFT);
		grid.getCellFormatter().setWidth(0, 2, "300px");
		grid.getCellFormatter().setVerticalAlignment(0, 3,
				HasAlignment.ALIGN_TOP);
		grid.getCellFormatter().setStyleName(0, 3, "pt");

		root.add(grid);

		initWidget(root);
	}

	public void setPerson(Person p) {
		person = p;
	}

	public void listMyNetworks() {
		flexyNetworks = new VerticalPanel();
		List<NetworkItem> list = person.getNetworkItems();

		for (NetworkItem item : list) {
			if (!item.getNetworkItemType().equalsIgnoreCase("website")
					&& !item.getNetworkItemType().equalsIgnoreCase("company")
					&& !item.getNetworkItemType().equalsIgnoreCase("blog")
					&& !item.getNetworkItemType().equalsIgnoreCase("other")) {
				getNetworks(item);
			}
		}
		root.add(flexyNetworks);
	}

	private void getNetworks(NetworkItem item) {
		NetworksImage images = (NetworksImage) GWT.create(NetworksImage.class);
		final HorizontalPanel hp = new HorizontalPanel();
		hp.setWidth("350px");
		hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		String type = item.getNetworkItemType();
		AbstractImagePrototype img = null;

		if (type.equalsIgnoreCase("linkedin"))
			img = images.linkedin();
		else if (type.equalsIgnoreCase("twitter"))
			img = images.twitter();
		else if (type.equalsIgnoreCase("facebook"))
			img = images.facebook();
		else if (type.equalsIgnoreCase("viadeo"))
			img = images.viadeo();
		else if (type.equalsIgnoreCase("flickr"))
			img = images.flickr();
		else if (type.equalsIgnoreCase("vimeo"))
			img = images.vimeo();
		else if (type.equalsIgnoreCase("dopplr"))
			img = images.dopplr();
		else if (type.equalsIgnoreCase("lastfm"))
			img = images.lastfm();
		else if (type.equalsIgnoreCase("readernaut"))
			img = images.readernaut();
		else if (type.equalsIgnoreCase("friendfeed"))
			img = images.friendfeed();

		/*
		 * image
		 */
		hp.add(img.createImage());
		/*
		 * link
		 */
		String url = item.getNetworkItemLink();
		Anchor anchor = new Anchor(url, url);
		hp.add(anchor);
		hp.setCellWidth(anchor, "300px");

		/*
		 * button delete
		 */
		PushButton remove = new PushButton(ergoImages.delete().createImage());
		remove.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				flexyNetworks.remove(hp);
			}
		});
		hp.add(remove);

		flexyNetworks.add(hp);
	}

	public void listAllNetworks() {
		FlexTable flex = new FlexTable();
		NetworksImage images = (NetworksImage) GWT.create(NetworksImage.class);

		for (int i = 0; i < stringsnetworks.length; i++) {
			if (stringsnetworks[i].equalsIgnoreCase("linkedin"))
				img = images.linkedin().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("twitter"))
				img = images.twitter().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("facebook"))
				img = images.facebook().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("viadeo"))
				img = images.viadeo().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("flickr"))
				img = images.flickr().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("vimeo"))
				img = images.vimeo().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("dopplr"))
				img = images.dopplr().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("lastfm"))
				img = images.lastfm().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("readernaut"))
				img = images.readernaut().createImage();
			else if (stringsnetworks[i].equalsIgnoreCase("friendfeed"))
				img = images.friendfeed().createImage();

			pb = new PushButton(img);
			pb.setPixelSize(40, 41);
			pb.setTitle(stringsnetworks[i]);
			pb.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					showNetworkDialog(((PushButton) event.getSource())
							.getTitle());
				}
			});

			flex.setWidget((i / 7), i % 7, pb);

		}
		root.add(flex);
	}

	public void showNetworkDialog(final String name) {
		final DialogBox netDialog = new DialogBox();

		final String title = name.substring(0, 1).toUpperCase()
				+ name.substring(1);
		netDialog.setText("Your public " + title + "'s profile url");

		VerticalPanel netPanel = new VerticalPanel();
		netPanel.setSpacing(5);
		netDialog.setWidget(netPanel);

		HorizontalPanel head = new HorizontalPanel();

		final TextBox url = new TextBox();
		url.setWidth("200px");

		NetworksImage images = (NetworksImage) GWT.create(NetworksImage.class);
		if (title.equalsIgnoreCase("linkedin")) {
			img = images.linkedin().createImage();
			url.setText("http://www.linkedin.com/in/");
		} else if (title.equalsIgnoreCase("twitter")) {
			img = images.twitter().createImage();
			url.setText("http://twitter.com/");
		} else if (title.equalsIgnoreCase("facebook"))
			img = images.facebook().createImage();
		else if (title.equalsIgnoreCase("viadeo"))
			img = images.viadeo().createImage();
		else if (title.equalsIgnoreCase("flickr")) {
			img = images.flickr().createImage();
			url.setText("http://www.flickr.com/photos/");
		} else if (title.equalsIgnoreCase("vimeo")) {
			img = images.vimeo().createImage();
			url.setText("http://www.vimeo.com/");
		} else if (title.equalsIgnoreCase("dopplr")) {
			img = images.dopplr().createImage();
			url.setText("http://www.dopplr.com/traveller/");
		} else if (title.equalsIgnoreCase("lastfm")) {
			img = images.lastfm().createImage();
			url.setText("http://www.lastfm.fr/user/");
		} else if (title.equalsIgnoreCase("readernaut")) {
			img = images.readernaut().createImage();
			url.setText("http://readernaut.com/");
		} else if (title.equalsIgnoreCase("friendfeed")) {
			img = images.friendfeed().createImage();
			url.setText("http://friendfeed.com/");
		}

		head.add(img);
		HTML label = new HTML(title + "'s profile url :", true);
		head.add(label);
		head.setCellVerticalAlignment(label, HasAlignment.ALIGN_MIDDLE);
		head.setSpacing(5);

		netPanel.add(head);
		netPanel.add(url);

		HorizontalPanel buttons = new HorizontalPanel();
		buttons.setSpacing(10);

		Button cancel = new Button("Cancel");
		cancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				netDialog.hide();
			}
		});

		Button ok = new Button("OK");
		ok.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				String content = url.getText();
				String type = title;

				NetworkItem ni = new NetworkItem();
				ni.setNetworkItemLink(content);
				ni.setNetworkItemType(type);
				getNetworks(ni);

				netDialog.hide();
			}

		});

		buttons.add(cancel);
		buttons.add(ok);

		netPanel.add(buttons);
		netPanel.setCellHorizontalAlignment(buttons, HasAlignment.ALIGN_RIGHT);

		netDialog.center();
		url.setFocus(true);
		url.setCursorPos(url.getText().length());// position the cursor at the
													// end of the textbbox
		netDialog.show();

	}

	public ArrayList<?> getDatas() {
		networks = new ArrayList<NetworkItem>();
		receiveData();
		return networks;
	}

	private void receiveData() {
		int length = flexyWebsites.getWidgetCount();
		for (int i = 0; i < length; i++) {
			HorizontalPanel hp = (HorizontalPanel) flexyWebsites.getWidget(i);
			int selected = ((ListBox) hp.getWidget(0)).getSelectedIndex();
			String content = ((TextBox) hp.getWidget(1)).getText();
			saveDatas(selected, content);
		}
		int netl = flexyNetworks.getWidgetCount();
		for (int i = 0; i < netl; i++) {
			HorizontalPanel hp = (HorizontalPanel) flexyNetworks.getWidget(i);
			String content = ((Anchor) hp.getWidget(1)).getHref();
			saveDatas(0, content);
		}
	}

	private void saveDatas(int selected, String content) {
		if (content != null && !content.equals("")) {

			NetworkItem n = new NetworkItem();
			n.setNetworkItemLink(content);

			String type = content;
			if (type.contains("linkedin"))
				n.setNetworkItemType("linkedin");
			else if (type.contains("twitter"))
				n.setNetworkItemType("twitter");
			else if (type.contains("facebook"))
				n.setNetworkItemType("facebook");
			else if (type.contains("viadeo"))
				n.setNetworkItemType("viadeo");
			else if (type.contains("flickr"))
				n.setNetworkItemType("flickr");
			else if (type.contains("vimeo"))
				n.setNetworkItemType("vimeo");
			else if (type.contains("dopplr"))
				n.setNetworkItemType("dopplr");
			else if (type.contains("lastfm"))
				n.setNetworkItemType("lastfm");
			else if (type.contains("readernaut"))
				n.setNetworkItemType("readernaut");
			else if (type.contains("friendfeed"))
				n.setNetworkItemType("friendfeed");
			else {
				switch (selected) {
				case 0:
					n.setNetworkItemType("website");
					break;
				case 1:
					n.setNetworkItemType("company");
					break;
				case 2:
					n.setNetworkItemType("blog");
					break;
				case 3:
					n.setNetworkItemType("other");
					break;
				}
			}
			networks.add(n);
		}
	}

	public void setRowsNetworkItem(List<NetworkItem> list) {
		removeAll();
		flexyNetworks = new VerticalPanel();

		for (NetworkItem item : list) {
			if (item.getNetworkItemType().equalsIgnoreCase("website"))
				addRow(0, item.getNetworkItemLink());
			else if (item.getNetworkItemType().equalsIgnoreCase("company"))
				addRow(1, item.getNetworkItemLink());
			else if(item.getNetworkItemType().equalsIgnoreCase("blog"))
				addRow(2, item.getNetworkItemLink());
			else if (item.getNetworkItemType().equalsIgnoreCase("other"))
				addRow(3, item.getNetworkItemLink());
			 else {
				getNetworks(item);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void setRows(List<?> list) {

		if (list != null && list.size() > 0) {
			setRowsNetworkItem((List<NetworkItem>) list);
		}
	}

	public void removeAll() {
		flexyWebsites = new VerticalPanel();
		grid.setWidget(0, 2, flexyWebsites);
	}

	/**
	 * Add a row to the flex table.
	 */
	private void addRow() {
		addRow(null, null);
	}

	private void addRow(Integer type, String content) {
		// final int numRows = flexyTable.getRowCount();
		// final int numRows = flexyWebsites.getWidgetCount();
		final HorizontalPanel hp = new HorizontalPanel();
		// String content = null;

		ListBox choices = new ListBox();
		choices.setWidth("100px");

		choices.addItem("My Website");
		choices.addItem("My Company");
		choices.addItem("My Blog");

		choices.addItem("Other");
		if (type != null) {
			choices.setSelectedIndex(type);
		}
		hp.add(choices);
		// flexyTable.setWidget(numRows, 0, choices);

		TextBox text = new TextBox();
		text.setWidth("165px");
		text.setFocus(true);
		if (content != null) {
			text.setValue(content);
		}
		hp.add(text);

		PushButton remove = new PushButton(ergoImages.delete().createImage());
		remove.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				flexyWebsites.remove(hp);
				if (flexyWebsites.getWidgetCount() == 0)
					addRow();
			}
		});
		hp.add(remove);
		flexyWebsites.add(hp);
	}
}